﻿using System;
using System.Collections;

using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.Samples.DirectX.UtilityToolkit;

using System.Drawing;

namespace Simple2DSample
{

    /// <summary>
    /// This class represents a sprite that moves randomly around a predefined space in screen coordinates
    /// </summary>
    public partial class MovingSprite
    {
        private float direction;
        /// <summary>
        /// In this example, position is calculated from the center of the sprite
        /// </summary>
        private Vector2 position;
        private float screenWidth, screenHeight;
        private float spriteWidth, spriteHeight;
        public  Rectangle srcRect;
        private float speed;
        private Random rand;
        private Texture tex;
        private const float PI = (float)Math.PI;

        private float timeSinceTurn;
        private const float timeToTurn = 1f;
        private float aliveTime;


        /// <summary>
        /// This class represents a sprite that moves randomly around a predefined space in screen coordinates
        /// </summary>
        /// <param name="spriteTexture">The texture used for the sprite</param>
        /// <param name="sourceRect">The source rectangle of the sprite to be drawn</param>
        /// <param name="random">An instance of the Random class</param>
        /// <param name="movementSpeed">The speed at which the sprite moves.</param>
        public MovingSprite(Texture spriteTexture, Rectangle sourceRect, Random random, float movementSpeed)
        {
            rand = random;
            speed = movementSpeed;
            srcRect = sourceRect;
            tex = spriteTexture;
            spriteWidth = sourceRect.Width;
            spriteHeight = sourceRect.Height;
        }

        /// <summary>
        /// Sets the size of the screen in which the moving sprite can animate
        /// </summary>
        public void SetScreenSize(int width, int height)
        {
            screenWidth = width;
            screenHeight = height;
            position.X = screenWidth / 2;
            position.Y = screenHeight / 2;
            direction = (float)rand.NextDouble() * 2f * PI;
            direction = 0;
        }



        /// <summary>
        /// This method uses Draw2D to easily draw the sprite to screen space.
        /// </summary>
        public void Draw(Sprite sprite)
        {
            sprite.Draw2D(tex, srcRect,
                //The destination size
                new SizeF(spriteWidth, spriteHeight),
                //The center point (For Draw2D, this value is relative to the source image, not the scaled end image)
                new PointF(srcRect.Width / 2f, srcRect.Height / 2f),
                //The angle of rotation is simply the direction of the sprite
                direction,
                //the position is calculated from the center of the sprite
                new PointF(position.X, position.Y),
                Color.White);
        }

        /// <summary>
        /// This draw call example is not used in the sample, but is provided to show
        /// how to emulate the Draw2D call by setting the transformation property.
        /// before the Draw() call.
        /// </summary>
        public void DrawAlternate(Sprite sprite)
        {
            Matrix transformation =
                //First, translate to the center of the original (untransformed) sprite
                Matrix.Translation(-srcRect.Width / 2f, -srcRect.Height / 2f, 0f)
                //scale the sprite
                * Matrix.Scaling(spriteWidth / srcRect.Width, spriteHeight / srcRect.Height, 1)
                //rotate about the z axis (into the screen) of the centered sprite
                * Matrix.RotationZ(direction)
                //translate to the final position
                * Matrix.Translation(position.X, position.Y, 0);

            //set the transformation
            sprite.Transform = transformation;
            //Draw with position 0, center 0, and color white (all needed transformation has been completed at this point)
            sprite.Draw(tex, srcRect, Vector3.Empty, Vector3.Empty, Color.Red);
        }
    }
}
